package com.t309.tzy.common;
import com.t309.tzy.entity.TAddress;
import com.t309.tzy.entity.TSysUser;
import org.springframework.format.Formatter;
import org.springframework.util.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Locale;
/**
 *
 */
public class TSysUserFormatter implements Formatter<TSysUser> {

    @Override
    public TSysUser parse(String s, Locale locale) throws ParseException {
        TSysUser user = new TSysUser();
        //获取class对象
        Class<? extends TSysUser> aClass = user.getClass();
        String[] splits = s.split(",");
        for(String split: splits){
            String[] kvs = split.split(":");
            try {
                Method method = aClass.getDeclaredMethod("set" + StringUtils.capitalize(kvs[0]),String.class);
                method.invoke(user,kvs[1]);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return user;
    }
    @Override
    public String print(TSysUser user, Locale locale) {
        System.out.println("realName="+user.getRealName());
        return user.getRealName();
    }
}
